#include <QtCore/QCoreApplication>
#include <Windows.h>
#include <stdio.h>
#include <cstdlib>
#include <ostream>
#include <locale>
#include <conio.h>
#include "Database.h"
#include "Telephone.h"

HANDLE hPipe;
Database data;

void append()
{
	DWORD readed;
	DWORD written;
	int result;
	structBase base;
	Telephone data_;
	ReadFile(hPipe, (void*)&base, sizeof(structBase), &readed, NULL);
	data_.name = QString(base.name);
	data_.name_orient = base.name_orient;
	data_.sizes = QString(base.sizes);
	data_.radio = base.radio;
	data_.card =  base.card;
	data_.net = base.net;
	data_.haveprice = base.haveprice;
	data_.other = QString(base.other);
	data_.first = base.first;
	data_.price = base.price;
	data_.camera = base.camera;
	data_.id = base.id;
	result = data.append(data_);
	WriteFile(hPipe, (void*)&result, sizeof(int), &written, NULL);
}

void count()
{
	int count = data.count();
	DWORD written;
	WriteFile(hPipe, (void*)&count, sizeof(int), &written, NULL);
}

void records()
{
	DWORD readed;
	DWORD written;
	QVector<PhoneItem> records;
	PhoneItem2 record;
	records = data.records();
	int i, n;
	n = records.size();
	WriteFile(hPipe, (void*)&n, sizeof(int), &written, NULL);
	for (i = 0; i < n; i++)
	{
		strcpy(record.name, records[i].name.toStdString().c_str());
		record.name_orient = records[i].name_orient;
		record.price = records[i].price;
		record.id = records[i].id;
		WriteFile(hPipe, (void*)&record, sizeof(PhoneItem2), &written, NULL);
	}

}

void save()
{
	DWORD written;
	bool result;
	data.save();
	WriteFile(hPipe, (void*)&result, sizeof(bool), &written, NULL);
}

void update()
{
	DWORD readed;
	DWORD written;
	int result;
	structBase base;
	Telephone data_;
	ReadFile(hPipe, (void*)&base, sizeof(structBase), &readed, NULL);
	data_.name = QString(base.name);
	data_.name_orient = base.name_orient;
	data_.sizes = base.sizes;
	data_.radio = base.radio;
	data_.card = base.card;
	data_.net = base.net;
	data_.haveprice = base.haveprice;
	data_.first = data_.first;
	data_.price = base.price;
	data_.id = base.id;
	result = data.update(data_);
	WriteFile(hPipe, (void*)&result, sizeof(int), &written, NULL);

}

void remove()
{
	int id;
	DWORD readed;
	ReadFile(hPipe, (void*)&id, sizeof(int), &readed, NULL);
	data.remove(id);
}

void record()
{
	int id;
	DWORD readed;
	DWORD written;
	Telephone data_;
	structBase base;
	int result;
	ReadFile(hPipe, (void*)&id, sizeof(int), &readed, NULL);
	data_ = data.record(id);

	/*if (data_ == NULL)
	{
		result = 0;
		WriteFile(hPipe, (void*)&result, sizeof(int), &written, NULL);
	}
	else
	{*/
		result = 1;
		strcpy(base.name, data_.name.toStdString().c_str());
		base.name_orient = data_.name_orient;
		strcpy(base.sizes ,data_.sizes.toStdString().c_str());
		base.radio = data_.radio;
		base.card = data_.card;
		base.net = data_.net;
		base.haveprice = data_.haveprice;
		strcpy(base.other, data_.other.toStdString().c_str());
		base.first = data_.first;
		base.price = data_.price;
		base.camera = data_.camera;
		base.id = data_.id;
		WriteFile(hPipe, (void*)&result, sizeof(int), &written, NULL);
		WriteFile(hPipe, (void*)&base, sizeof(structBase), &written, NULL);

}

void load()
{
	DWORD written;
	bool result;
	result = data.load();
	WriteFile(hPipe, (void*) &result, sizeof(bool), &written, NULL);
}

void clear()
{
	data.clear();
}

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	setlocale(LC_ALL, "Rus");
	//freopen("log.txt","a+", stdout);
	hPipe = CreateNamedPipe(L"\\\\.\\pipe\\Phone", 
		PIPE_ACCESS_DUPLEX, 
		PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT /*| PIPE_UNLIMITED_INSTANCES*/, 
		254,
		1024, 
		1024, 
		5000, 
		NULL);
	if (hPipe == INVALID_HANDLE_VALUE)
	{
		printf("don't creat Pipe");
		return 0;
	}
	printf("Pipe created. Waiting for clients...");
	ConnectNamedPipe(hPipe, NULL);
	printf("client connected!\n");

	while (1)
	{
		char c;
		DWORD readed;
		ReadFile(hPipe, (void*)&c, sizeof(char), &readed, NULL);
		if (c == 0)
		{
			count();
		}
		if (c ==1)
		{
			append();
			printf("\\\\add record\n");
		}
		if (c == 2)
		{
			remove();
			printf("\\\\remove record\n");
		}
		if (c ==3)
		{
			update();
			printf("\\\\update record\n");
		}
		if (c == 4)
		{
			record();
		}
		if (c == 5)
		{
			records();
		}
		if (c == 6)
		{
			save();
			printf("\\\\save record\n");
		}
		if (c == 7)
		{
			load();
		}
		if (c == 8)
		{
			clear();
		}
	}
	printf("End!");
	DisconnectNamedPipe(hPipe);
	CloseHandle(hPipe);
	
	getch();
	return a.exec();
}
